In this section, you can explore the data on your own and discover which variables are more interesting to you. The purpose of this exploration is to allow you to investigate different counties and see if you find anything noteworthy. In subsequent pages, we will delve into more specific aspects of the data.

The data provided includes information on bridges in the United States, categorized by their condition (poor, fair, or good) and the total bridge area in square meters. You can visualize this data using choropleth maps, which display the data based on geographic regions, in this case, counties.

The choropleth maps are interactive, allowing you to zoom in and out, pan around the map, and toggle different data layers on and off. Each layer represents a specific variable, such as the total number of bridges or the total bridge area in a particular condition.

By exploring these maps, you can gain insights into the distribution of bridges across the country, identify regions with a higher concentration of bridges in poor or fair condition, and compare the total bridge area across different counties.

Feel free to interact with the maps, switch between different data layers, and observe any patterns or outliers that catch your attention. This exploration will serve as a foundation for further analysis and discussion in the subsequent pages.

Data Preparation

In the data preparation part, we perform several key steps to prepare the data for visualization. We load the bridge data from a CSV file and define a mapping for renaming and scaling the variables. This mapping helps to provide more meaningful names and scales the values appropriately for better visualization. We apply the renaming and scaling to the data based on the mapping.

Additionally, we define a color scheme for each category of bridge condition (poor, fair, good) to ensure consistent color representation across the choropleth maps.

Lastly, we fetch the US county GeoJSON data from a URL and filter out specific states (Hawaii, Alaska, and Washington D.C.) to focus on the contiguous United States.

These data preparation steps ensure that the data is in a suitable format for creating the interactive choropleth maps in the subsequent sections.

Code
import pandas as pd
import folium
from folium.features import GeoJsonTooltip
import requests
Code
# Path to your CSV file
csv_file_path = '../data/data.csv'
variable_options = [
    'Total bridges',
    'Bridges, poor',
    'Bridges, fair',
    'Bridges, good',
    'Bridge area (square meters)',
    'Bridge area, poor (square meters)',
    'Bridge area, fair (square meters)',
    'Bridge area, good (square meters)'
]

df = pd.read_csv(csv_file_path)
# df.columns
Code
# Mapping for renaming and scaling
name_scale_mapping = {
    'Total bridges': {
        'new_name': 'Total Bridges (100)',
        'scale': 100
    },
    'Bridges, poor': {
        'new_name': 'Total Poor Bridges (100)',
        'scale': 100
    },
    'Bridges, fair': {
        'new_name': 'Total Fair Bridges (100)',
        'scale': 100
    },
    'Bridges, good': {
        'new_name': 'Total Good Bridges (100)',
        'scale': 100
    },
    'Bridge area (square meters)': {
        'new_name': 'Total Bridge Area (10k m²)',
        'scale': 10000
    },
    'Bridge area, poor (square meters)': {
        'new_name': 'Total Poor Bridge Area (10k m²)',
        'scale': 100000
    },
    'Bridge area, fair (square meters)': {
        'new_name': 'Total Fair Bridge Area (10k m²)',
        'scale': 100000
    },
    'Bridge area, good (square meters)': {
        'new_name': 'Total Good Bridge Area (10k m²)',
        'scale': 100000
    },
}

# Apply renaming and scaling based on the mapping
for original_name, props in name_scale_mapping.items():
    updated_rows = df['Variable'] == original_name  # Capture the rows to update
    df.loc[updated_rows, 'Variable'] = props['new_name']
    df.loc[updated_rows, 'Value'] /= props['scale']  # Apply scaling only to the updated rows

variable_options = [props['new_name'] for props in name_scale_mapping.values()]

# Define color scheme for each category
color_scheme = {
    'Total': 'Blues',
    'Poor': 'Reds',
    'Fair': 'Oranges',
    'Good': 'Greens'
}
Code
folium_counties_url = "https://raw.githubusercontent.com/python-visualization/folium/main/tests/us-counties.json"
us_counties = requests.get(folium_counties_url).json()

# Define state FIPS codes to drop (Hawaii, Alaska, DC)
fips_to_drop = ['02', '15', '11']  # FIPS codes for AK, HI, DC respectively

# Filter out counties based on their state FIPS code
us_counties['features'] = [
    feature for feature in us_counties['features']
    if feature['id'].zfill(5)[:2] not in fips_to_drop  # Ensure IDs are 5 digits for uniform comparison
]

Data Presentation

Code
# Create a folium Map object, centered on the US, *without* a tile layer yet
m = folium.Map(
    [43, -100],
    zoom_start=4,
    tiles=None
)
# # Add the TileLayer to m *separately*, with control=False so that users
# # are not able remove the layer by unchecking a checkbox
folium.TileLayer('CartoDB positron',name="Light Map",control=False).add_to(m)
<folium.raster_layers.TileLayer at 0x7fe7190dfb50>
Code
# Loop through each variable option to create separate choropleth layers
for variable in variable_options:
    subset_df = df[df['Variable'] == variable]
    legend_label = variable.replace('_', ' ').title()
    if 'Poor' in variable:
        color = color_scheme['Poor']
    elif 'Fair' in variable:
        color = color_scheme['Fair']
    elif 'Good' in variable:
        color = color_scheme['Good']
    else:  # Default to 'Total' for any other cases, typically 'Total'
        color = color_scheme['Total']

    choro = folium.Choropleth(
        geo_data=us_counties,
        name=variable,
        data=subset_df,
        columns=['GEOID', 'Value'],  # Changed from ['State', 'Value']
        key_on='feature.id',  # Ensure this matches your county GeoJSON
        fill_color=color,
        fill_opacity=0.7,
        line_opacity=0.2,
        legend_name=legend_label,
        highlight=True,
        show=(variable == 'Total Bridges (100)'),
        overlay=False
    ).add_to(m)

    # Add tooltips to each layer
    tooltip = GeoJsonTooltip(
        fields=['name'],
        aliases=['County:'],
        localize=True,
        sticky=False,
        labels=True,
        style="""
            background-color: #F0EFEF;
            border: 2px solid black;
            border-radius: 3px;
            box-shadow: 3px;
        """,
        max_width=800,
    )
    choro.geojson.add_child(tooltip)
Code
folium.LayerControl(collapsed=False).add_to(m)
<folium.map.LayerControl at 0x7fe7190ddb40>
Code
m
Make this Notebook Trusted to load map: File -> Trust Notebook
Code
m.save('.fullscreen_explore_map.html')